package com.dhecp.project.storage.service.impl;
import java.math.BigDecimal;
import java.util.Date;

import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.project.storage.domain.GaugeMaintenance;
import com.dhecp.project.storage.domain.HwStorage;
import com.dhecp.project.storage.domain.StockControl;
import com.dhecp.project.storage.mapper.HwStorageMapper;
import com.dhecp.project.storage.service.HwStorageService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import java.util.List;
import java.util.Map;

/**
 * 海外仓库存管理(HwStorage表)服务实现类
 *
 * @author fxz
 * @since 2024-02-21 15:53:39
 */
@Service("hwStorageService")
public class HwStorageServiceImpl implements HwStorageService {
    @Autowired
    private HwStorageMapper hwStorageMapper;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**
     * 通过guid查询单条数据
     *
     * @param params 对象
     * @return 实例对象
     */
    @Override
    public HwStorage queryFirstRecord(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return this.hwStorageMapper.selectFirstRecordByNid(params);
    }

    /**
     * 新增海外仓库存
     */
    @Override
    public void editHwStorage(StockControl stockControl, GaugeMaintenance gaugeMaintenance,Integer type,Integer addUpType) {
        HwStorage hwStorage = new HwStorage();
        hwStorage.setSku("C3426-"+stockControl.getProductCode());
        hwStorage.setCommoditycode(stockControl.getProductCode());
        hwStorage.setCommodityname(stockControl.getProductName());
        hwStorage.setBoxnum(stockControl.getProductNum()/gaugeMaintenance.getNum());
        hwStorage.setCartonsize(gaugeMaintenance.getLength()+ "*" + gaugeMaintenance.getWeight() + "*" + gaugeMaintenance.getHeight());
        hwStorage.setSingleboxnum(gaugeMaintenance.getNum());
        hwStorage.setProductnum(stockControl.getProductNum());
        hwStorage.setProductnumall(stockControl.getProductNumAll());
        hwStorage.setSdays(stockControl.getsDays());
        hwStorage.setDrksj(stockControl.getdRkSj());
        hwStorage.setVhw(stockControl.getvHw());
        hwStorage.setVkq(stockControl.getvKq());
        hwStorage.setGdepot(stockControl.getgDepot());
        hwStorage.setVgsjc(stockControl.getvGsJc());
        hwStorage.setVgsbm(stockControl.getvGsBm());
        hwStorage.setCube((gaugeMaintenance.getLength().multiply(gaugeMaintenance.getWeight()).multiply(gaugeMaintenance.getHeight())).divide(new BigDecimal(1000000)).multiply(new BigDecimal(hwStorage.getBoxnum())));
        hwStorage.setStype(type);

        if (addUpType == 1){
            hwStorage.setStatus(0);
            hwStorage.setCreatetime(new Date());
            hwStorage.setGuid(RandomValueUtils.getGUID());
            hwStorage.setKcId(stockControl.getGuid());
            hwStorageMapper.insert(hwStorage);
        }else {
            hwStorage.setUpdatetime(new Date());
            hwStorageMapper.update(hwStorage);
        }
    }

    /**
    * 查询列表数据
    */
    @Override
    public List<HwStorage> queryList(Map<String, Object> params) throws Exception{
    params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return this.hwStorageMapper.select(params);
    }
}

